<template>
  <el-upload ref="uploader"
             style="display: inline-block"
             class="upload-demo"
             action="#"
             :accept="accept"
             :before-upload="beforeUpload"
             :http-request="() => {}"
             :show-file-list="false">
    <el-button type="primary"
               :loading="loading">
      {{ status ? "导入模板" : "重新导入" }}
    </el-button>
  </el-upload>
</template>
<script>
var sheetType = ".xlsx,.xls,.cvs";
export default {
  name: "UploadExcel",
  props: {
    api: {
      type: Object
    },
  },
  data () {
    return {
      userInfo: sessionStorage.getItem('userInfo'),
      accept: sheetType,
      loading: false,
      status: 1
    };
  },
  computed: {
    user () {
      return JSON.parse(this.userInfo)
    }
  },
  created () { },
  methods: {
    //文件上传
    beforeUpload (file) {
      const param = new FormData();
      param.append("file", file);
      this.importExcel(param);
    },
    async importExcel (file) {
      this.loading = true;
      this.$emit("messageStatus", 2);
      let response = await this.api.importExcel(file).catch(() => {
        this.loading = false;
      });
      if (response?.code === 12531) {
        this.loading = false;
        this.$emit("messageStatus", 1);
        this.$message.error(response?.message);
        return
      }
      this.loading = false;
      const { imports, name } = response?.data
      if (imports.length === 0) {
        this.$message.warning("导入模板未填写数据")
      }

      this.$emit("messageStatus", { status: 3, imports: imports, name: name });
      // this.$store.commit("orderImport/ORDERIMPORTNAME", { name: response?.data.name, userId: this.user?.userId });
      // this.$store.commit("orderImport/ORDERIMPORT", { importData: response?.data.imports, userId: this.user?.userId });
    }
  }
};
</script>
